package com.zzyl.controller;

import com.zzyl.base.PageResponse;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.NursingPlanDto;
import com.zzyl.service.NursingPlanService;
import com.zzyl.vo.NursingPlanVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class NursingPlanController extends BaseController {

    @Autowired
    private NursingPlanService nursingPlanService;

    /**
     * 新增护理计划
     * @param dto
     * @return
     */
    @PostMapping("/nursing/plan")
    public ResponseResult savePlan(@RequestBody NursingPlanDto dto){
//        一次操作2张表
        nursingPlanService.save(dto);
        return success();
    }

    /**
     * 条件分页查询
     * @param name
     * @param status
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/nursing/plan/search")
    public ResponseResult<PageResponse<NursingPlanVo>> findByPage(
            @RequestParam(name = "name",required = false) String name,
            @RequestParam(name = "status",required = false) Integer status,
            @RequestParam(name = "pageNum",defaultValue = "1") Integer pageNum,
            @RequestParam(name = "pageSize",defaultValue = "10") Integer pageSize
    ){
        PageResponse<NursingPlanVo> page = nursingPlanService.findByPage(name,status,pageNum,pageSize);
        return success(page);
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @GetMapping("/nursing/plan/{id}")
    public ResponseResult<NursingPlanVo> findById(@PathVariable(name = "id") Long id){
        NursingPlanVo vo = nursingPlanService.findById(id);
        return success(vo);
    }

    /**
     * 修改护理计划
     * @param dto
     * @return
     */
    @PutMapping("/nursing/plan/{id}")
    public ResponseResult update(@RequestBody NursingPlanDto dto){
        nursingPlanService.update(dto);
        return success();
    }

    /**
     * 删除护理计划
     * @param id
     * @return
     */
    @DeleteMapping("/nursing/plan/{id}")
    public ResponseResult deleteById(@PathVariable(name = "id") Long id){
        nursingPlanService.delete(id);
        return success();
    }

    /**
     * 护理计划的启用和禁用
     * @param id
     * @param status
     * @return
     */
    @PutMapping("/nursing/{id}/status/{status}")
    public ResponseResult updateStatus(@PathVariable(name = "id") Long id,
                                       @PathVariable(name = "status") Integer status){
        nursingPlanService.updateStatus(id,status);
        return success();
    }

    /**
     * 查询所有护理计划
     * @return
     */
    @GetMapping("/nursing/plan")
    public ResponseResult<List<NursingPlanVo>> findByAll(){
        List<NursingPlanVo> list = nursingPlanService.findByAll();
        return success(list);
    }
}
